# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/13
# @File   : R29_EC_UpaidBusinessOfConcCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请企业未结清业务五级分类校验（关注类）
code:R29_EC_UpaidBusinessOfConcCheck
tableName:
columnName:
groups:企业征信校验
dependencies:EC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def unpaidPaidBusinessCheckPre(tableName):
    table = getTable(tableName)
    if table is None:
        return None
    elif table == [{}]:
        return 0
    else:
        result = []
        flag = []
        for i in table:
            try:
                if i['ReportNo'] not in null_type_list:
                    result.append(i['ReportNo'])
            except:
                flag.append(None)
                continue
        if len(flag) == len(table):
            return None
        else:
            return len(result)

def R29_EC_UpaidBusinessOfConcCheck():
    # 处理 未结清信贷信息概要(7类信贷业务)
    summaryList = {}
    ECRSummaryUnsettled02 = getTable('ECRSummaryUnsettled02')
    if ECRSummaryUnsettled02 is None:
        summaryList = None
    elif ECRSummaryUnsettled02 == [{}]:
        summaryList.update({u'贷款':0, u'贸易融资':0, u'保理':0,
                            u'银行承兑汇票':0, u'信用证':0, u'保函':0, u'类贷款': 0})
    else:
        flag = []
        for i in ECRSummaryUnsettled02:
            try:
                if i['LoanType'] == u'贷款':
                    if i['Concernedsum_Number'] not in null_type_list and isfloat(i['Concernedsum_Number']):
                        tmp = float(i['Concernedsum_Number'])
                        summaryList.update({u'贷款': tmp})
                    else:
                        summaryList.update({u'贷款': 0})
                        flag.append(None)

                elif i['LoanType'] == u'贸易融资':
                    if i['Concernedsum_Number'] not in null_type_list and isfloat(i['Concernedsum_Number']):
                        tmp = float(i['Concernedsum_Number'])
                        summaryList.update({u'贸易融资': tmp})
                    else:
                        summaryList.update({u'贸易融资': 0})
                        flag.append(None)
                elif i['LoanType'] == u'保理':
                    if i['Concernedsum_Number'] not in null_type_list and isfloat(i['Concernedsum_Number']):
                        tmp = float(i['Concernedsum_Number'])
                        summaryList.update({u'保理': tmp})
                    else:
                        summaryList.update({u'保理': 0})
                        flag.append(None)
                elif i['LoanType'] == u'银行承兑汇票':
                    if i['Concernedsum_Number'] not in null_type_list and isfloat(i['Concernedsum_Number']):
                        tmp = float(i['Concernedsum_Number'])
                        summaryList.update({u'银行承兑汇票': tmp})
                    else:
                        summaryList.update({u'银行承兑汇票': 0})
                        flag.append(None)
                elif i['LoanType'] == u'信用证':
                    if i['Concernedsum_Number'] not in null_type_list and isfloat(i['Concernedsum_Number']):
                        tmp = float(i['Concernedsum_Number'])
                        summaryList.update({u'信用证': tmp})
                    else:
                        summaryList.update({u'信用证': 0})
                        flag.append(None)
                elif i['LoanType'] == u'保函':
                    if i['Concernedsum_Number'] not in null_type_list and isfloat(i['Concernedsum_Number']):
                        tmp = float(i['Concernedsum_Number'])
                        summaryList.update({u'保函': tmp})
                    else:
                        summaryList.update({u'保函': 0})
                        flag.append(None)
                elif i['LoanType'] == u'类贷款':
                    if i['Concernedsum_Number'] not in null_type_list and isfloat(i['Concernedsum_Number']):
                        tmp = float(i['Concernedsum_Number'])
                        summaryList.update({u'类贷款': tmp})
                    else:
                        summaryList.update({u'类贷款': 0})
                        flag.append(None)
                else:
                    pass

            except:
                flag.append(None)

        # 除 票据贴现
        if len(flag) == 7:
            summaryList = None

    print summaryList
    #处理Factor1
    infoList1 = unpaidPaidBusinessCheckPre('ECRUnpaidSMLoanInfo')

    if infoList1 == None and summaryList == None:
        Factor1 = None
    else:
        if infoList1 == None:
            Factor1 = max(summaryList.get(u'贷款'), 0)
        elif summaryList == None:
            Factor1 = max(0, infoList1)
        else:
            Factor1 = max(summaryList.get(u'贷款'), infoList1)

    #处理Factor2
    infoList2 = unpaidPaidBusinessCheckPre('ECRUnpaidSMTradeFinancing')

    if infoList2 == None and summaryList == None:
        Factor2 = None
    else:
        if infoList2 == None:
            Factor2 = max(summaryList.get(u'贸易融资'), 0)
        elif summaryList == None:
            Factor2 = max(0, infoList2)
        else:
            Factor2 = max(summaryList.get(u'贸易融资'), infoList2)

    # 处理Factor3
    infoList3 = unpaidPaidBusinessCheckPre('ECRUnpaidSMFactoring')
    if infoList3 == None and summaryList == None:
        Factor3 = None
    else:
        if infoList3 == None:
            Factor3 = max(summaryList.get(u'保理'), 0)
        elif summaryList == None:
            Factor3 = max(0, infoList3)
        else:
            Factor3 = max(summaryList.get(u'保理'), infoList3)

    # 处理Factor4
    infoList4 = unpaidPaidBusinessCheckPre('ECRUnpaidSMBankAcceptance')
    if infoList4 == None and summaryList == None:
        Factor4 = None
    else:
        if infoList4 == None:
            Factor4 = max(summaryList.get(u'银行承兑汇票'), 0)
        elif summaryList == None:
            Factor4 = max(0, infoList4)
        else:
            Factor4 = max(summaryList.get(u'银行承兑汇票'), infoList4)

    # 处理Factor5
    infoList5 = unpaidPaidBusinessCheckPre('ECRUnpaidSMLetterCredit')
    if infoList5 == None and summaryList == None:
        Factor5 = None
    else:
        if infoList5 == None:
            Factor5 = max(summaryList.get(u'信用证'), 0)
        elif summaryList == None:
            Factor5 = max(0, infoList5)
        else:
            Factor5 = max(summaryList.get(u'信用证'), infoList5)

    # 处理Factor6
    infoList6 = unpaidPaidBusinessCheckPre('ECRUnpaidSMLetterGuarantee')
    if infoList6 == None and summaryList == None:
        Factor6 = None
    else:
        if infoList6 == None:
            Factor6 = max(summaryList.get(u'保函'), 0)
        elif summaryList == None:
            Factor6 = max(0, infoList6)
        else:
            Factor6 = max(summaryList.get(u'保函'), infoList6)

    # 处理Factor7 类贷款
    if summaryList == None:
        Factor7 = None
    else:
        Factor7 = summaryList.get(u'类贷款')


    #处理7类业务明细和汇总表均为None 返回 缺失值
    if Factor1 is None and Factor2 is None and Factor3 is None \
            and Factor4 is None and Factor5 is None and Factor6 is None and Factor7 is None:
        return u'缺失值'
    else:
        if Factor1 is None:
            Factor1 = 0
        if Factor2 is None:
            Factor2 = 0
        if Factor3 is None:
            Factor3 = 0
        if Factor4 is None:
            Factor4 = 0
        if Factor5 is None:
            Factor5 = 0
        if Factor6 is None:
            Factor6 = 0
        if Factor7 is None:
            Factor7 = 0
        return Factor1 + Factor2 + Factor3 + Factor4 + Factor5 + Factor6 + Factor7


result = R29_EC_UpaidBusinessOfConcCheck()